import requests
from lxml import etree

num = 1
while True:
    url = 'https://www.zongheng.com/api2/catefine/storeSearch'

    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/135.0.0.0'
                      'Safari/537.36 Edg/135.0.0.0',
        # cookie
        'Cookie': 'ZHID=8F12792F68BB6B59D4FA7C121FF9C0A6; v_user=https%3A%2F%2Fwww.zongheng.com%2F%7Chttps%3A%2F%2Fhuayu.zongheng.com%2F%7C22620529; zh_visitTime=1744116244377; PassportCaptchaId=9c20e2f89a2664c46694a9ead8737803; zhffr=0; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22196156dd176112b-0674d4bc8d33f88-4c657b58-2073600-196156dd177125e%22%2C%22%24device_id%22%3A%22196156dd176112b-0674d4bc8d33f88-4c657b58-2073600-196156dd177125e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; Hm_lvt_c202865d524849216eea846069349eb9=1744116244,1744200706; HMACCOUNT=77A73498F77AD706; acw_sc__v2=67f6564079df05d9c2a4f75f9c359efbfed4ec95; Hm_lpvt_c202865d524849216eea846069349eb9=1744202085',

    }
    payload = {
        "worksTypes": 0,
        "bookType": 1,
        "subWorksTypes": 0,
        "totalWord": 0,
        "serialStatus": 1,
        "vip": 9,
        "pageNum": num,
        "pageSize": 20,
        "categoryId": 0,
        "categoryPid": 0,
        "naodongFilter": 0
    }
    req = requests.post(url, headers=header, params=payload)

    for i in req.json()['result']['bookList']:
        urls = f"https://huayu.zongheng.com/showchapter/{i['bookId']}.html"
        req = requests.get(urls, headers=header)
        tree = etree.HTML(req.text)
        links = tree.xpath('//li[@class=" col-4"]/a/@href')
        titles = tree.xpath('//li[@class=" col-4"]/a/text()')
        h1 = tree.xpath('//h1/text()')
        print(h1)
        print(f"章节: {titles}")
        for link in links:
            content = requests.get(link, headers=header)
            tree = etree.HTML(content.text)
            yue = tree.xpath('//div[@class="content"]/p/text()')
            print(yue)
        num += 1
